package com.tz.crm.service.impl;


import com.tz.crm.dao.RoleDao;
import com.tz.crm.dao.UserDao;
import com.tz.crm.entity.Role;
import com.tz.crm.entity.User;
import com.tz.crm.service.UserService;
import com.tz.crm.utils.Pager;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * UserService
 * 员工的增改查
 */
@Service
@Transactional
@SuppressWarnings("unchecked")
public class UserServiceImpl extends BaseServiceImpl<User, String> implements
        UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;

    /**
     * 有参构造
     *
     * @param userDao 注入的 UserDao 组件
     */
    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
        super.setBaseDao(userDao);
    }

    @Override
    public List<User> getByName(String realName) {
        return userDao.getByName(realName);
    }

    @Override
    public List<User> getAll(Pager pager) {
        return userDao.getAll(pager);
    }

    @Override
    public User getByLogin(String username, String password) {
        return userDao.login(username, password);
    }

    @Override
    public List<User> getByDepartmentName(String name, Pager pager) {
        return userDao.getByDepartmentName(name, pager);
    }

    @Override
    public Pager<User> getByPager(Pager pager, User user) {
        if (user == null) {
            user = new User();
        }
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        //根据部门id精确匹配
        if (user.getDept() != null && StringUtils.isNotBlank(user.getDept().getId())) {
            dc.add(Restrictions.eq("dept.id", user.getDept().getId().trim()));
        }
        //根据用户名模糊匹配
        if (StringUtils.isNotBlank(user.getRealName())) {
            dc.add(Restrictions.like("realName", user
                    .getRealName(), MatchMode.ANYWHERE).ignoreCase());
        }
        pager = userDao.findByPager(pager, dc, Order.asc("realName"));
        return pager;
    }

    @Override
    public Pager<User> getUsersByPager(Pager pager, String departmentId) {
        DetachedCriteria dc = DetachedCriteria
                .forClass(User.class);
        if (StringUtils.isNotBlank(departmentId)) {
            dc.add(Restrictions.eq("dept.id", departmentId.trim()));
        }
        pager = userDao.findByPager(pager, dc, Order.asc("realName"));
        return pager;
    }

    @Override
    public void saveWithRole(User user, String roleIds) {
        String[] idArray = roleIds.split(",");
        List<Role> roleList = roleDao.get(idArray);
        for (Role r : roleList) {
            user.getRoleSet().add(r);
        }
        user.setPassword(user.getUsername());
        userDao.save(user);
    }

    @Override
    public List<Role> getRoleListByUserId(String id) {
        List<Role> list = new ArrayList<Role>();
        User user = userDao.get(id);
        for (Role r : user.getRoleSet()) {
            list.add(r);
        }
        return list;
    }

    @Override
    public void updateWithRole(User user, String roleIds) {

        User nuser=userDao.get(user.getId());
        String[] idArray = roleIds.split(",");
        List<Role> roleList = roleDao.get(idArray);
        Set<Role> roleSet=new HashSet<>();
        for (Role r : roleList) {
            nuser.getRoleSet().add(r);
        }
        nuser.setPassword(nuser.getUsername());
        userDao.update(nuser);
    }

    @Override
    public void updatePersonalInfo(User user) {
        User u = userDao.get(user.getId());
        u.setBirthDate(user.getBirthDate());
        u.setRealName(user.getRealName());
        u.setGender(user.getGender());
        u.setTelephone(user.getTelephone());
        u.setRemarks(user.getRemarks());
        u.setMaxCustomer(user.getMaxCustomer());
        u.setTelephone(user.getTelephone());
        u.setDept(user.getDept());
        u.setUsername(user.getUsername());
        u.setValid(user.isValid());
        userDao.update(u);
    }


}
